package Utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;

public class FileUtil {

    private static FileSystem fs;

    public FileUtil(String path)
            throws IOException {
        Configuration conf = new Configuration();
        fs = FileSystem.get(URI.create(path), conf);
    }

    public FileUtil(Configuration conf, String path)
            throws IOException {
        fs = FileSystem.get(URI.create(path), conf);
    }

    public boolean fileExists(String path)
            throws IOException {
        if (fs.exists(new Path(path))) {
            System.out.println(path + " file already exists!");
            return true;
        }
        return false;
    }

    public void doDelete(String path)
            throws IOException {
        if(fileExists(path)){
            System.out.println("now deleting ...");
            fs.delete(new Path(path),true);
        }
    }

    public void doError(String path)
            throws IOException {
        if(!fileExists(path)){
            System.out.println("file not exist, now exiting ...");
            System.exit(1);
        }
    }

}
